package com.whosly.action.log.registrar;

import com.whosly.api.autoconfigure.context.ISpringContextAware;
import com.whosly.api.autoconfigure.context.SpringContextAwareHolder;
import com.whosly.api.autoconfigure.holder.AnnotationMapsHolder;
import com.whosly.action.log.api.DefaultActionServiceImpl;
import com.whosly.action.log.api.IActionService;
import com.whosly.action.log.autoconfigure.RActionAspect;
import com.whosly.action.log.data.AutoActionConfigData;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
//@Configuration(proxyBeanMethods = false)
// or
//@Import({ RActionAspect.class })
public class AutoActionConfiguration {

    /**
     * SpringContextHolderExt 的默认实例化。
     * 如果已经存在 SpringContextHolderExt 的实例，则不初始化该操作
     */
    @Bean
    @ConditionalOnMissingBean(ISpringContextAware.class)
    public ISpringContextAware springContextAware() {
        ISpringContextAware springContextHolderExt = new SpringContextAwareHolder();

        return springContextHolderExt;
    }

    /**
     * IActionService 的默认实例化。
     * 如果已经存在 IActionService 的实例，则不初始化该操作
     */
    @Bean
    @ConditionalOnMissingBean(IActionService.class)
    public IActionService actionService() {
        DefaultActionServiceImpl actionService = new DefaultActionServiceImpl();

        return actionService;
    }

    @Bean
    public RActionAspect rActionAspect() {
        AnnotationMapsHolder<AutoActionConfigData> holder = AnnotationMapsHolder.INSTANCE(AutoActionConfigRegistrar.BEAN_NAME);
        AutoActionConfigData configData = holder.get();

        RActionAspect actionAspect = new RActionAspect(configData);

        return actionAspect;
    }

}
